/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bs.extensoesSwing;

import com.bs.persistencia.EntityManager;
import java.awt.Window;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JOptionPane;

/**
 *
 * @author Bone
 */
public class FormularioController<E> {

    private E bean;
    private EntityManager entityManager;
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private Window janela;
    private boolean podeIncluir = true;
    private boolean podeExcluir = true;
    private boolean podeAlterar = true;

    public FormularioController() {
    }

    public E getBean() {
        return bean;
    }

    public void setBean(E bean) {
        E old = this.bean;
        this.bean = bean;
        changeSupport.firePropertyChange("bean", old, bean);
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        EntityManager old = this.entityManager;
        this.entityManager = entityManager;
        changeSupport.firePropertyChange("entityManager", old, entityManager);
    }

    public Window getJanela() {
        return janela;
    }

    public void setJanela(Window janela) {
        Window old = this.janela;
        this.janela = janela;
        changeSupport.firePropertyChange("janela", old, janela);
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
        changeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        changeSupport.removePropertyChangeListener(l);
    }

    public void alterar() {
        if (!entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().begin();
        }
        entityManager.alterar(bean);
        entityManager.getTransaction().commit();
        System.out.println("Item alterado com sucesso.");
        janela.dispose();
    }

    public void incluir() {
        if (!entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().begin();
        }
        entityManager.inserir(bean);
        entityManager.getTransaction().commit();
        System.out.println("Item incluido com sucesso.");
        janela.dispose();
    }

    public void excluir() {
        int result = JOptionPane.showConfirmDialog(janela, "Deseja realmente excluir este item?", "Exclusão de item", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.NO_OPTION) {
            return;
        }
        if (!entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().begin();
        }
        entityManager.excluir(bean);
        entityManager.getTransaction().commit();
        System.out.println("Item excluido com sucesso.");
        janela.dispose();
    }

    public void cancelar() {
        janela.dispose();
        if (janela instanceof PadraoForm) {
            ((PadraoForm) janela).setCancelado(true);
        }
    }

    public boolean isPodeIncluir() {
        return podeIncluir;
    }

    public void setPodeIncluir(boolean podeIncluir) {
        boolean old = this.podeIncluir;
        this.podeIncluir = podeIncluir;
        changeSupport.firePropertyChange("podeIncluir", old, podeIncluir);
    }

    public boolean isPodeExcluir() {
        return podeExcluir;
    }

    public void setPodeExcluir(boolean podeExcluir) {
        boolean old = this.podeExcluir;
        this.podeExcluir = podeExcluir;
        changeSupport.firePropertyChange("podeExcluir", old, podeExcluir);
    }

    public boolean isPodeAlterar() {
        return podeAlterar;
    }

    public void setPodeAlterar(boolean podeAlterar) {
        boolean old = this.podeAlterar;
        this.podeAlterar = podeAlterar;
        changeSupport.firePropertyChange("podeAlterar", old, podeAlterar);
    }
}
